const { Controller } = require('varal');

class SchoolController extends Controller {
	async index() {
		//queryString.parse取法通过region获取数组，只能通过region[0],region[1]单独获取；
		const { name, 'region[0]': province_id, 'region[1]': city_id, page = 1, page_size = 20 } = this.app.fields;
		const serv = this.app.service('SchoolService');

		return serv.get({
			name,
			province_id,
			city_id
		}, page, page_size).then(({ data, total }) => {
			this.app.json({
				err_code: 0,
				data: {
					data,
					total
				}
			})
		})
	}

	async info() {
		const { id } = this.app.fields;
		const serv = this.app.service('SchoolService');

		const data = await serv.info({ id });

		this.app.json({
			data
		})
	}

	async delete() {
		const { id } = this.app.fields;

		const serv = this.app.service('SchoolService');

		return serv.delete(id).then(() => {
			this.app.json({
				err_code: 0
			})
		})
	}

	async save() {
		const params = this.app.fields;

		const serv = this.app.service('SchoolService');

		return serv.save(params).then(() => {
			this.app.json({
				err_code: 0
			})
		}).catch((err) => {
			this.app.json({
				err_code: 1,
				err_msg: err.toString()
			})
		});

	}
}

exports = module.exports = SchoolController;
